package com.sf.business.appointment.service.impl;

import com.sf.business.appointment.domain.BusAppointment;
import com.sf.business.appointment.domain.BusCheckout;
import com.sf.business.appointment.mapper.BusAppointmentMapper;
import com.sf.business.appointment.mapper.BusCheckoutMapper;
import com.sf.business.appointment.service.IBusAppointmentService;
import com.sf.business.appointment.util.AssertArgumentsUtil;
import com.sf.business.appointment.vo.BusAppointmentVO;
import com.sf.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 养修信息预约Service业务层处理
 * 
 * @author lh
 * @date 2025-03-16
 */
@Service
public class BusAppointmentServiceImpl implements IBusAppointmentService 
{
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;
    @Autowired
    private BusCheckoutMapper busCheckoutMapper;

    /**
     * 查询养修信息预约
     * 
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public BusAppointment selectBusAppointmentById(Long id)
    {
        BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        return busAppointment;
    }

    /**
     * 查询养修信息预约列表
     * 
     * @param busAppointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment)
    {
        return busAppointmentMapper.selectBusAppointmentList(busAppointment);
    }

    /**
     * 新增养修信息预约
     * 
     * @param vo 养修信息预约
     * @return 结果
     */
    @Override
    public int insertBusAppointment(BusAppointmentVO vo)
    {
        AssertArgumentsUtil.assertArguments(vo);

        BusAppointment busAppointment = new BusAppointment();
        BeanUtils.copyProperties(vo, busAppointment);

        busAppointment.setCreateTime(DateUtils.getNowDate());
        busAppointment.setStatus(BusAppointment.STATUS_APPOINTING);
        return busAppointmentMapper.insertBusAppointment(busAppointment);
    }


    /**
     * 修改养修信息预约
     * 
     * @param vo 养修信息预约
     * @return 结果
     */
    @Override
    public int updateBusAppointment(BusAppointmentVO vo)
    {
        AssertArgumentsUtil.assertArguments(vo);

        BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(vo.getId());
        if (busAppointment == null)
            throw new IllegalArgumentException("数据不存在");

        if (busAppointment.getStatus() != BusAppointment.STATUS_APPOINTING)
            throw new IllegalArgumentException("数据已处理，不能修改 (状态为预约中才支持修改) ");

        BeanUtils.copyProperties(vo, busAppointment);
        return busAppointmentMapper.updateBusAppointment(busAppointment);
    }

    /**
     * 批量删除养修信息预约
     * 
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentByIds(Long[] ids)
    {
        return busAppointmentMapper.deleteBusAppointmentByIds(ids);
    }

    /**
     * 删除养修信息预约信息
     * 
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentById(Long id)
    {
        BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        if (busAppointment == null)
            throw new IllegalArgumentException("数据不存在");

        if (busAppointment.getStatus() != BusAppointment.STATUS_APPOINTING && busAppointment.getStatus() != BusAppointment.STATUS_CANCEL && busAppointment.getStatus() != BusAppointment.STATUS_TIMEOUT_CANCEL)
            throw new IllegalArgumentException("数据不能删除 (状态 不对 [ 预约中, 用户取消, 超时取消 ] ) ");

        return busAppointmentMapper.deleteBusAppointmentById(id);
    }

    @Override
    public void cancel(Long id) {
        BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        if (busAppointment == null)
            throw new IllegalArgumentException("数据不存在");

        if (busAppointment.getStatus() != BusAppointment.STATUS_APPOINTING)
            throw new IllegalArgumentException("数据不能取消 (状态 不对 [ 预约中 ] ) ");

        busAppointment.setStatus(BusAppointment.STATUS_CANCEL);
        busAppointmentMapper.updateBusAppointment(busAppointment);
    }

    @Override
    public void toStore(Long id) {
        BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        if (busAppointment == null)
            throw new IllegalArgumentException("数据不存在");

        if (busAppointment.getStatus() != BusAppointment.STATUS_APPOINTING)
            throw new IllegalArgumentException("数据不能取消 (状态 不对 [ 预约中 ] ) ");

        busAppointment.setStatus(BusAppointment.STATUS_ARRIVAL);
        busAppointment.setActualArrivalTime(DateUtils.getNowDate());
        busAppointmentMapper.updateBusAppointment(busAppointment);
    }

    @Override
    public Long generateCheckoutById(Long id) {
        BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        if (busAppointment == null)
            throw new IllegalArgumentException("预约 数据不存在");
        if (busAppointment.getStatus() != BusAppointment.STATUS_ARRIVAL && busAppointment.getStatus() != BusAppointment.STATUS_SETTLEMENT_GENERATED && busAppointment.getStatus() != BusAppointment.STATUS_PAYMENT_GENERATED)
            throw new IllegalArgumentException("预约 数据状态不对, 需要 [已到店 / 结算单已生成 / 已支付 ] ");

        BusCheckout busCheckout = busCheckoutMapper.selectBusCheckoutByAppointmentId(id);
        if (busCheckout == null){
            busCheckout = new BusCheckout();
            BeanUtils.copyProperties(busAppointment, busCheckout);
            busCheckout.setId(null);
            busCheckout.setStatus(BusCheckout.STATUS_CONSUMING);
            busCheckout.setAppointmentId(id);
            busCheckoutMapper.insertBusCheckout(busCheckout);
            busAppointment.setStatus(BusAppointment.STATUS_SETTLEMENT_GENERATED);
            busAppointmentMapper.updateBusAppointment(busAppointment);
        }

        return busCheckout.getId();
    }
}
